//
//  XKVisitoView.swift
//  XK2WeiBo
//
//  Created by mac on 16/7/6.
//  Copyright © 2016年 陈肖坤. All rights reserved.
//

import UIKit
import SnapKit

class XKVisitoView: UIView {

    //button按钮点击
    var closure :(()->())?
    
    override init(frame:CGRect) {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置视图
    fileprivate func setupUI() {
        backgroundColor = RBG(237,green: 237,blue: 237)
        
        //添加控件
        addSubview(feedImageView)
        addSubview(feedMaskView)
        addSubview(iconImageView)
        addSubview(messageLadel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //圆圈约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //遮罩约束
        feedMaskView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //房子约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
         //文子约束
        messageLadel.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(feedMaskView.snp_bottom).offset(16)
            make.centerX.equalTo(feedMaskView)
        }
        
        //登录按钮约束
        loginButton.snp_makeConstraints { (make) -> Void in
        make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(messageLadel)
            make.top.equalTo(messageLadel.snp_bottom).offset(16)
        }
        
        //注册按钮约束
        registerButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.right.equalTo(messageLadel)
            make.top.equalTo(loginButton)
        }
        
        
    }
    
    //MARK: 设置动画
    func setupAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的时间
        anim.duration = 10
        //设置tovalue
        anim.toValue = M_PI * 2
        //动画的次数
        anim.repeatCount = MAXFLOAT
        //不让动画停止
        anim.isRemovedOnCompletion = false
        //添加动画
        feedImageView.layer.add(anim, forKey: nil)
    }
    
    //MARK: 共外界调用的方法
    func setupVisitorViewInfo(_ imageName: String?, title: String?) {
        
        if let image = imageName ,let title = title {
            iconImageView.image = UIImage(named: image)
            messageLadel.text = title
        
            //隐藏
            feedImageView.isHidden = true
        }else {
            //代表是主页
            setupAnim()
        }
        
    
    }
    
    @objc private func buttonClick() {
//        print("按钮点击了")
        closure?()
    }
    
    //MARK: 懒加载控件
    //圆圈visitordiscover_feed_image_smallicon
    fileprivate lazy var feedImageView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮罩visitordiscover_feed_mask_smallicon
    fileprivate lazy var feedMaskView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子 visitordiscover_feed_image_house
    fileprivate lazy var iconImageView:UIImageView = UIImageView(image:UIImage(named:  "visitordiscover_feed_image_house"))
    
    //文字 
    fileprivate lazy var messageLadel:UILabel = {
        let ladel = UILabel()
        ladel.textColor = ThemeColor
        ladel.text = "关注一些人,回这里看看有什么惊喜"
        ladel.numberOfLines = 0
        ladel.font = UIFont.systemFont(ofSize: 14)
        //设置对齐方式
        ladel.textAlignment = .center
        return ladel
    }()
    
    //登录按钮
    fileprivate lazy var loginButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(XKVisitoView.buttonClick), for: UIControlEvents.touchUpInside);
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState())
        button.setTitle("登录", for: UIControlState())
        button.setTitleColor(ThemeColor, for: UIControlState())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        return button
    }()
    
    //注册按钮
    fileprivate lazy var registerButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(XKVisitoView.buttonClick), for: UIControlEvents.touchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState())
        button.setTitle("注册", for: UIControlState())
        button.setTitleColor(ThemeColor, for: UIControlState())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return button
    }()
    
    
}

extension XKVisitoView {
    func demo(){
        feedImageView.translatesAutoresizingMaskIntoConstraints = false
        /*
        -item:约束的对象
        -attribute:约束条件
        -relatedBy:等于什么
        -toItem:相对的对象
        -attribute:约束条件
        -multiplier:倍数
        -constant:浮点数
        */
        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
    }
}

